package com.zzf.concurrent.example.design.pattern.count.down;

import com.zzf.concurrent.example.util.TimeUtil;
import lombok.extern.slf4j.Slf4j;

import java.util.stream.IntStream;

/**
 * @author GaoFeng2017
 * @date 2019/9/9 17:14
 */
@Slf4j
public class App {
    public static void main(String[] args) {

        CountDownLatch countDownLatch = new CountDownLatch(5);
        IntStream.rangeClosed(1, 5).forEach(index -> {
            new Thread(() -> {
                Thread thread = Thread.currentThread();
                log.info("{} start working...", thread);
                TimeUtil.sleep((long) (Math.random() * 1000));
                log.info("{} is finished.", thread);
                countDownLatch.countDown();
            }).start();
        });


        try {
            countDownLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        log.info("first task is finished, now start second task");
    }
}
